package exp07gui.mine.v1;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MineFrame extends JFrame implements ActionListener {
    private static final int MINE = 9;
    static Random r = new Random();
    protected int height, width, mines, remain, board[][];
    protected JButton btns[][];

    public MineFrame(int height, int width, int mines) {
        super("Mime ver 1.0 by ZHAO Jing");
        this.height = height;
        this.width = width;
        this.mines = mines;
        remain = height * width - mines;
        board = new int[height + 2][width + 2];
        placeMime();

        btns = new JButton[height][width];
        setLayout(new GridLayout(width, height));
        for (int i = 0; i < height; i++)
            for (int j = 0; j < width; j++) {
                add(btns[i][j] = new JButton());
                btns[i][j].addActionListener(this);
            }
        this.setSize(height * 50 + 90, width * 50 + 25);
        this.setVisible(true);
    }

    private void placeMime() {
        for (int i = 0; i < mines; ) {
            int x = r.nextInt(height) + 1;
            int y = r.nextInt(width) + 1;
            if (board[x][y] == 0) {
                ++i;
                board[x][y] = MINE;
            }
        }
        for (int i = 1; i <= height; i++)
            for (int j = 1; j <= width; j++)
                if (board[i][j] != MINE)
                    board[i][j] = count8(i, j);
    }

    private int count8(int h, int w) {
        int s = 0;
        for (int i = h - 1; i <= h + 1; i++)
            for (int j = w - 1; j <= w + 1; j++)
                if (board[i][j] == MINE)
                    ++s;
        return s;
    }

    public static void main(String[] args) {
        new MineFrame(10, 10, 2);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton) {
            JButton curButton = (JButton) (e.getSource());
            int x = 0, y = 0;
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                    if (btns[i][j] == curButton) {
                        x = i + 1;
                        y = j + 1;
                    }
            if (board[x][y] == MINE)
                JOptionPane.showMessageDialog(null, "Game over!");
            else if (board[x][y] > 0) {
                --remain;
                curButton.setText(board[x][y] + "");
                curButton.setEnabled(false);
            } else expand(x, y);
            if (remain == 0) // �ж�ʤ��
                JOptionPane.showMessageDialog(null, "Win");
        }
    }

    private void expand(int x, int y) {
        --remain;
        if (board[x][y] != 0) {
            btns[x - 1][y - 1].setText(board[x][y] + "");
            btns[x - 1][y - 1].setEnabled(false);
            return;
        }
        btns[x - 1][y - 1].setText(board[x][y] + "");
        btns[x - 1][y - 1].setEnabled(false);
        int h = x, w = y;
        for (int i = h - 1; i <= h + 1; i++)
            for (int j = w - 1; j <= w + 1; j++)
                if (ok(i, j) && btns[i - 1][j - 1].isEnabled())
                    expand(i, j);
    }

    private boolean ok(int x, int y) {
        return x <= height && x >= 1 && y <= width && y >= 1;
    }
}
